<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 100%;overflow-y: hidden">
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item  label="工程名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="请选择工程名称" v-decorator.trim="[ 'engineerName']"  :readOnly="true"/>
        </a-form-item>
        <a-form-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <a-select placeholder="请输入产品名称" v-decorator.trim="[ 'name']"  :dropdownMatchSelectWidth="false" 
           showSearch :autoClearSearchValue="true" optionFilterProp="children"  :allowClear="true" :not-found-content="null"  
           @change="handleChange" @search="handleSearch"  @blur="handleBlur" >

           <!-- <a-select 
				 v-model="name"
				:show-search="true" 
				:not-found-content="null" 
				:show-arrow="false" 
				:filter-option="true"
				:autoClearSearchValue="false" 
				@search="handleSearch" 
				@blur="handleBlur" 
				@change="handleChange"> -->

            <a-select-option v-for="(item,index) in produceList" :key="index" :value="item.name">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        
         <a-form-item label="产品型号" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <a-select placeholder="请选择产品型号" v-decorator.trim="[ 'model', validatorRules.model]" :dropdownMatchSelectWidth="false"  >
              <a-select-option  value="EPS线条">EPS线条 </a-select-option>
              <a-select-option  value="GRC线条">GRC线条 </a-select-option>
              <a-select-option  value="A级线条">A级线条 </a-select-option>
              <a-select-option  value="其他">其他 </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="产品规格宽度" :labelCol="labelCol" :wrapperCol="wrapperCol" >
           <a-input placeholder="请输入宽度："  v-decorator.trim="[ 'sizeX', validatorRules.sizeX]" suffix="mm"/>
        </a-form-item>
         <a-form-item label="产品规格高度" :labelCol="labelCol" :wrapperCol="wrapperCol" >
           <a-input placeholder="请输入高度："  v-decorator.trim="[ 'unitX', validatorRules.unitX]" suffix="mm"/>
        </a-form-item>
         <a-form-item label="产品规格长度" :labelCol="labelCol" :wrapperCol="wrapperCol" >
           <a-input placeholder="请输入长度："  v-decorator.trim="[ 'sizeY', validatorRules.sizeY]" suffix="mm"/>
        </a-form-item>
        <a-form-item  label="产品数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input-number placeholder="请输入产品数量" style="width: 100%" :min="0" v-decorator.trim="[ 'num', validatorRules.num]" />
        </a-form-item>
        <a-form-item  label="产品单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <a-select placeholder="请选择产品单位" v-decorator.trim="[ 'unit', validatorRules.unit]" :dropdownMatchSelectWidth="false"  >
              <a-select-option  value="米" >米</a-select-option>
              <a-select-option  value="个" >个</a-select-option>
            </a-select>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea :rows="4" placeholder="请输入备注" v-decorator="[ 'description' ]"  />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="产品造型">
           <!-- <div style="color:red">支持：.png,.jpg,.jpeg,.tiff,.gif,.bmp,.doc,.docx,.xlsx,.xls,.zip,.pdf</div> -->
           <j-image-upload v-model="fileList" bizPath="produce" text="上传" isMultiple></j-image-upload>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import Vue from 'vue'
  import JSelectPosition from '@/components/jeecgbiz/JSelectPosition'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import {addEngineerProduce,editEngineerProduce,checkEngineerProduce} from '@/api/api'
  import JDate from '@/components/jeecg/JDate'
  import JUpload from '@/components/jeecg/JUpload'
  import JImageUpload from '../../../components/jeecg/JImageUpload'
  import {EngineerMixin } from '../mixins/EngineerMixin'
  export default {
    name: "EngineerProduceModal",
    mixins:[EngineerMixin],
    components: {
      JImageUpload,
      JUpload,
      JDate,
      JSelectPosition
    },
    data () {
      return {
        title:"操作",
        visible: false,
        modalWidth:800,
        drawerWidth:700,
        fileList:[],
        id:"", 
        engineerId:"",
        isReadOnly: false,
        disableSubmit:false,
        dateFormat:"YYYY-MM-DD",
        flag:true,
        result: '',//选择下拉列表中选项后的值
        validatorRules:{
          engineerName:{
            rules: [{
              required: true, message: '请输入工程名称!'
            }
            ],
          },
           num:{
            rules: [{
              required: true, message: '请输入产品数量!'
            }
            ],
          },
          name:{
            rules: [{
              required: true, message: '请输入产品名称!'
            },
            { validator: this.validateProduceName}
            ],
          },
          sizeX:{
              rules:[{
              required: true,type: 'number', message: '请输入正确格式的产品宽度!', trigger: 'blur',
               transform (value) {
                  var moneyg = /^\d+$|^\d*\.\d+$/g
                  if (!moneyg.test(value) ) {
                    return false
                  }else{
                    return Number(value)
                  }
                }
            }],
          },
          sizeY:{
            rules:[{
              required: true,type: 'number', message: '请输入正确格式的产品高度!', trigger: 'blur',
               transform (value) {
                  var moneyg = /^\d+$|^\d*\.\d+$/g
                  if (!moneyg.test(value) ) {
                    return false
                  }else{
                    return Number(value)
                  }
                }
            }],
          },
        },
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        uploadLoading:false,
        confirmLoading: false,
        headers:{},
        form:this.$form.createForm(this)
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token}
    },
    methods: {
      add (record) {
        this.form.resetFields();
        this.visible = true;
        this.model = Object.assign({}, record);
       
        this.model.engineerName = record.name;
        this.engineerId = record.id;
        this.model.name = "";
        this.model.description = "";
        this.model.id = "";
        this.model.unit = "";
        this.editAfter(this.model);
      },
      edit (record) {
        this.form.resetFields();
        this.visible = true;
        this.model = Object.assign({}, record);
        this.editAfter(this.model);
      },
            //调用完edit()方法之后会自动调用此方法
      editAfter() {
        if (this.action === 'add') {
          this.fileList = []
        } else {
           setTimeout(() => {
            this.fileList = this.model.fileName
          }, 5)
          // this.fileList = this.model.fileName
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model,'engineerName','name','model','sizeX','sizeY','unitX','num','unit','description'))
          });
        }
        let params = {
            engineerId:this.engineerId
          }
         this.initEngineerProduce(params)
      },
        handleSearch(value){
          if(!!value){
            this.result = value  
          }
        },

        handleChange(value){
          this.flag = true;
          this.result = value;
          this.form.setFields({ name : {value:value}})
          for (var i = 0; i <this.produceList.length; i++) {
           var oneSubject=this.produceList[i]
            if(value===oneSubject.name){
              this.form.setFields({ model : {value:oneSubject.model}})
              this.form.setFields({ sizeX : {value:oneSubject.sizeX}})
              this.form.setFields({ sizeY : {value:oneSubject.sizeY}})
              this.form.setFields({ unitX : {value:oneSubject.unitX}})
              this.form.setFields({ unit : {value:oneSubject.unit}})
              this.form.setFields({ fileList : {value:oneSubject.fileList}})
              this.fileList = oneSubject.fileList
              this.flag = false;
            }
          }
          window.console.log("flag： "+this.flag)
          if(this.flag){//手动清空
              this.form.setFields({ model : {value:''}})
              this.form.setFields({ sizeX : {value:''}})
              this.form.setFields({ sizeY : {value:''}})
              this.form.setFields({ unitX : {value:''}})
              this.form.setFields({ unit : {value:''}})
              this.fileList = ''
          }
        },
        
        handleBlur(value){
          if(!!this.result){
            this.handleChange(this.result)
          }
        },
      close() {
        this.$emit('close');
        this.visible = false;
        this.disableSubmit = false;
      },
  
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            if(this.fileList && this.fileList.length > 0) {
              formData.fileName = this.fileList
            }else {
             formData.fileName = ''
            }
            formData.engineerId = this.engineerId;
            let obj;
            if(!this.model.id){
              formData.id = this.id;
              obj=addEngineerProduce(formData);
            }else{
              obj=editEngineerProduce(formData);
            }
            obj.then((res)=>{
              if(res.code === 200){
                that.$emit('ok');
              }else{
                that.$message.warning(res.data.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel() {
        this.close()
      },
      validateProduceName(rule, value, callback){
        let params = {
          name: value,
          id: this.model.id?this.model.id:0,
          engineerId: this.engineerId
        };
        checkEngineerProduce(params).then((res)=>{
          if(res && res.code===200) {
            if(!res.data){
              callback();
            } else {
              callback("名称已经存在");
            }
          } else {
            callback(res.data);
          }
        });
      }

    }
  }
</script>

<style scoped>

</style>